<link rel="stylesheet" type="text/css" href="<?php echo base_url()?>public/assets/css/h5/list.css">
<?php if (preg_match("/MicroMessenger/i", $_SERVER["HTTP_USER_AGENT"])) { ?>
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<?php } ?>
<style>
.detial p{padding:0%;}
</style>

<div class="detial" style="margin-top: 0px;">
    <p class="name">确认购买信息</p>
    
    <div class="weui_cells" style="margin-top: 0px;">
        <div class="weui_cell">
            <div class="weui_cell_bd weui_cell_primary">
                <p>商品名称</p>
            </div>
            <div class="weui_cell_ft"><?php echo $cp_info['title']?>（<?php echo $cp_info['hours']?>课时）</div>
        </div>
        <?php if($cp_info['give_hours'] != 0){?>
        <div class="weui_cell">
            <div class="weui_cell_bd weui_cell_primary">
                <p>赠送课时</p>
            </div>
            <div class="weui_cell_ft"><?php echo $cp_info['give_hours']?></div>
        </div>
        <?php }?>
        <?php if($cp_info['expiry_date'] != '0000-00-00'){?>
        <div class="weui_cell">
            <div class="weui_cell_bd weui_cell_primary">
                <p>有效期至</p>
            </div>
            <div class="weui_cell_ft"><?php echo $cp_info['expiry_date']?></div>
        </div>
        <?php }?>
        <div class="weui_cell">
            <div class="weui_cell_bd weui_cell_primary">
                <p>所属园所</p>
            </div>
            <div class="weui_cell_ft"><?php echo $nursery_info['title']?></div>
        </div>
        <div class="weui_cell">
            <div class="weui_cell_bd weui_cell_primary">
                <p>上门范围</p>
            </div>
            <div class="weui_cell_ft"><?php echo $nursery_info['explain']?></div>
        </div>
        <div class="weui_cell">
            <div class="weui_cell_bd weui_cell_primary">
                <p>购买数量</p>
            </div>
            <div  class="weui_cell_ft">
                <div class="weui_cell_bd weui_cell_primary">
                    <select class="weui_select" name="pay_num" id="pay_num">
                        <option selected value="1">1</option>
                        <option value="2">2</option>
                        <option value="3">3</option>
                        <option value="4">4</option>
                        <option value="5">5</option>
                        <option value="6">6</option>
                        <option value="7">7</option>
                        <option value="8">8</option>
                        <option value="9">9</option>
                        <option value="10">10</option>
                    </select>
                </div>
                <span class="trangle"><i class="fa fa-caret-right"></i></span>
            </div>
        </div>
        <div class="weui_cell">
            <div class="weui_cell_bd weui_cell_primary">
                <p>支付方式</p>
            </div>
            <div  class="weui_cell_ft">
                <div class="weui_cell_bd weui_cell_primary">
                    <select class="weui_select" name="pay_type" id="pay_type">
                        <option selected value="1">在线支付</option>
                        <option value="2">划课</option>
                    </select>
                </div>
                <i class="fa fa-caret-right"></i>
            </div>
        </div>
        <div class="weui_cell">
            <div class="weui_cell_bd weui_cell_primary">
                <p>支付额度</p>
            </div>
            <div class="weui_cell_ft"><label class="cp_sale"><?php echo $cp_info['sale_price']?>元／</label><label class="cp_hours"><?php echo $cp_info['hours']?></label>节园所课</div>
        </div>
        <div class="weui_cell">
            <div class="weui_cell_bd weui_cell_primary">
                <p>订单总计</p>
            </div>
            <div class="weui_cell_ft"><label class="cp_sale_z"><?php echo $cp_info['sale_price']?>元／</label><label class="cp_hours_z"><?php echo $cp_info['hours']?></label>节园所课</div>
        </div>
    </div>
    <a href="javascript:void(0);" cp_id="<?php echo $cp_info['id'];?>" class="pay_btn weui_btn weui_btn_default">确认支付</a>
</div>

       
<div class="weui_dialog_alert" id="dialog_err" style="display:none;">
<div class="weui_mask"></div>
<div class="weui_dialog">
    <div class="weui_dialog_hd"></div>
    <div class="weui_dialog_ft">
        <a href="javascript:void(0);" class="weui_btn_dialog primary">确定</a>
    </div>
</div>
</div>

<!--未登录框-->
<div class="weui_dialog_confirm" id="dialog_login" style="display: none;">
    <div class="weui_mask"></div>
    <div class="weui_dialog">
        <br>
        <div class="weui_dialog_hd"><strong class="weui_dialog_title">您还没有登录</strong></div>
        <br>
        <div class="weui_dialog_ft">
            <a href="javascript:void(0);" class="weui_btn_dialog default">取消</a>
            <?php 
            $get = $this->input->get() ? $this->input->get() : array();
            $r_url = base_url() . uri_string() ."?". http_build_query($get);
            $return_url = base_url().'login'.'?url='. urlencode($r_url);
            ?>
            <a href="<?php echo $return_url?>" class="weui_btn_dialog primary">现在去登录</a>
        </div>
    </div>
</div>

<!--老师不能购买-->
<div class="weui_dialog_alert" id="dialog_teacher" style="display: none;">
    <div class="weui_mask"></div>
    <div class="weui_dialog">
        <div class="weui_dialog_hd"><strong class="weui_dialog_title"></strong></div>
        <div class="weui_dialog_bd">老师不支持买课</div>
        <div class="weui_dialog_ft">
            <a href="javascript:void(0);" class="weui_btn_dialog primary">确定</a>
        </div>
    </div>
</div>
        
<script type="text/javascript">
    //调用微信JS api 支付
    function jsApiCall(jsApiParameters, out_trade_no)
    {
        WeixinJSBridge.invoke(
            'getBrandWCPayRequest',
            jsApiParameters,
            function (res) {
                if (res.err_msg == 'get_brand_wcpay_request:ok') {
                    document.location = '<?php echo base_url()?>order/weixin_return?out_trade_no=' + out_trade_no + '&result=ok';
                }
            }
        );
    }
    
    function callpay(jsApiParameters, out_trade_no)
    {
        if (typeof WeixinJSBridge == "undefined") {
            if (document.addEventListener) {
                document.addEventListener('WeixinJSBridgeReady', jsApiCall(jsApiParameters, out_trade_no), false);
            } else if (document.attachEvent) {
                document.attachEvent('WeixinJSBridgeReady', jsApiCall(jsApiParameters, out_trade_no));
                document.attachEvent('onWeixinJSBridgeReady', jsApiCall(jsApiParameters, out_trade_no));
            }
        } else {
            jsApiCall(jsApiParameters, out_trade_no);
        }
    }
    

    $(document).ready(function(){

    	$(".weui_dialog_confirm .default").click(function(){
            $(this).parents(".weui_dialog_confirm").hide();
        });

        $(".weui_dialog_alert .primary").click(function(){
            $(this).parents(".weui_dialog_alert").hide();
        });
        
        $('#pay_num').change(function(){
            $('.cp_sale_z').text(<?php echo $cp_info['sale_price']?>*$(this).val() + '元/');
            $('.cp_hours_z').text(<?php echo $cp_info['hours']?>*$(this).val());

            if($("#pay_type").val() == 2){
            	$('.cp_sale, .cp_sale_z').hide();
            }else{
            	$('.cp_sale, .cp_sale_z').show();
            	
            }
        });

        $('#pay_type').change(function(){
            if($(this).val() == 2){
            	$('.cp_sale, .cp_sale_z').hide();
            }else{
            	$('.cp_sale, .cp_sale_z').show();
            }
        });
        
        $('.pay_btn').click(function () {
        	if(!<?php echo $this->_uid?>){
                $("#dialog_login").show();
                return false;
            }else if(<?php echo $this->_is_teacher?>){
            	$("#dialog_teacher").show();
                return false;
            }
            
            var pay_type = $("#pay_type").val();
            var cp_id = $(this).attr('cp_id');
            var pay_num = $("#pay_num").val();

            $.ajax({
                type: "post",
                url: "<?php echo base_url()?>order/weixin_wap_pay",
                data: {'cp_id':cp_id,'buy_num':pay_num,'pay_type':pay_type},
                dataType: 'json',
                success: function (data) {
                	if (data['code'] == 'E_1000') {
                		document.location = '<?php echo base_url()?>order/pay_success/' + data['out_trade_no'] ;
                    }else if (data['code'] == 'E_504') {
                    	$("#dialog_login").show();
                    } else if (data['code'] != 'E_200') {
                        $('#pay_btn').removeClass('no');
                        $("#dialog_err").find('.weui_dialog_hd').text(data['msg']);
                	    $("#dialog_err").show();
                    } else {
                        callpay(data['msg'], data['out_trade_no']);
                    }
                }
            });
        });

    });

   

</script>
